package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.AtributosDAO;
import com.cs.beneficios.business.model.AtributoVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.common.entity.Consultas;
import com.cs.beneficios.common.util.Atributo;
import com.cs.beneficios.web.form.ModificarForm;
//import com.cs.beneficios.web.form.PropiedadesAtributosForm;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.cs.paoyer.administration.business.model.UserVO;

public class ModificarAtriAction extends Action
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    Consultas objCons = new Consultas(cf);
    try{
        ModificarForm ActualizarAtri = (ModificarForm) form;
        Atributo objAtri = new Atributo();
       
        objAtri.setEtiqueta(ActualizarAtri.getEtiqueta());
        objAtri.setNombre(ActualizarAtri.getNombre());
        objAtri.setDescripcion(ActualizarAtri.getDescripcion());
        objAtri.setOrden(ActualizarAtri.getOrden());
        objAtri.setTipoatributo(ActualizarAtri.getTipoatributo());
        objAtri.setTipodato(ActualizarAtri.getTipodato());
        objAtri.setLongitud(ActualizarAtri.getLongitud());
        objAtri.setObligatorio(ActualizarAtri.getObligatorio());
        objAtri.setValLista(ActualizarAtri.getValLista());
        objAtri.setVigencia(ActualizarAtri.getVigencia());
        objAtri.setCodatributo(ActualizarAtri.getCodatributo());
        objAtri.setTipovalorflex(ActualizarAtri.getTipovalorflex());
        objAtri.setValorsugerido(ActualizarAtri.getCodigovfl());
        
        String strOrden = ActualizarAtri.getOrden();
        
        String strEliDominio = ActualizarAtri.getValLista();
    
        HttpSession session = request.getSession();
                //Proceso para la auditoria
         UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
         String terminalAddress = request.getRemoteAddr();
         String strTerminal = ("WEB="+terminalAddress);
         Calendar c = Calendar.getInstance(); 
         SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
        //Fin del proceso

        
        String strSubpro = "";
        Object oSubprog = (Object)session.getAttribute("subpro");
        if(oSubprog != null)
        {
          strSubpro= (String)session.getAttribute("subpro");
        }else
        {
          strSubpro= (String)request.getParameter("codigo");
        }
       
        String strCodAtri = request.getParameter("codatributo");
        
        AtributosDAO atriDAO = new AtributosDAO(cf);
        AtributoVO atriVO = new AtributoVO(); 
        try
        {
          
          atriVO = atriDAO.GetPropiedadesAtri(strCodAtri);
          cf.closeConnection(atriDAO.getConn());
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
        }
        
        int Orden = 0;
        int Nomb = 0;
        int iLong = 0;
        
        if(!atriVO.getNombre().equals(objAtri.getNombre()))
        {
           Nomb = objCons.getNombre(objAtri.getNombre(),strSubpro);
        }
        if(!atriVO.getOrden().equals(objAtri.getOrden()))
        {
          Orden = objCons.getOrden(objAtri.getOrden(),strSubpro);
         // if(Integer.parseInt(objAtri.getOrden().trim())<301)
         // {
         //  Orden = 1;
         // }
        }
        if(!(atriVO.getLongitud()!=null?atriVO.getLongitud():"0").equals(objAtri.getLongitud()))
        {
          if(Integer.parseInt(objAtri.getLongitud().trim())>500)
          {
            iLong = 1;
          }
        }
        
        
        if (Orden == 0 && Nomb == 0 && iLong == 0)
        {
            session.setAttribute("Error","2");
            String vallista = ActualizarAtri.getValLista();
            String tipovalorfl = ActualizarAtri.getTipovalorflex();
            String strArrAct[] = {user.getName(), dateFormat.format(c.getTime()),strTerminal};
            int actualizoBien = objCons.ModificarAtriSub(objAtri,strArrAct);
 
            //prr�ximas 3 l�neas agregadas 10-10-08 por Claudia
            if(actualizoBien == -1){
                session.setAttribute("Error","3");
            }else if(actualizoBien == -2){
                session.setAttribute("Error","4");
            }

            //proceso eliminar
            if(strEliDominio.equals("N"))
            {
              List lGettvfl = new LinkedList();
              lGettvfl = (List)objCons.GetTvflAtri(strCodAtri);
              Object obTvfl = (Object)lGettvfl.get(0);
              Object obCampo = (Object)lGettvfl.get(1);
              if(obTvfl != null)
              {
                String strTvfl = (String)lGettvfl.get(0);
                int iActTvfDominio = objCons.ActualizarTvfDominio(strCodAtri);
                
                //Proceso para validar si es un atributo generico tipo lista
                int verificar = objCons.getlistaGeneral(strTvfl);
                //fin del proceso
                if(verificar != 1)
                {
                  int iEliminarbien = objCons.EliminarVFLTotal(strTvfl);
                  session.setAttribute("msg","Desabilitacion correcta");
                }else
                {
                  session.setAttribute("msg","No se puede desabilitar valores de la lista para un atributo general");
                }
                /*
                if(obCampo != null)
                {
                  String strCodGene = (String)lGettvfl.get(1);
                  int iActTvfDominioge = objCons.ActualizarTvfDominioGene(strCodGene);
                }*/
                
                // int iEliminartvflbien = objCons.EliminarTVFLTotal(strTvfl);
              }
            }
            
            //fin proceso
            if(vallista.equals("S") && tipovalorfl.equalsIgnoreCase(""))
            {
              HttpSession escoger = request.getSession();
              int valor=2;
              String stropc=String.valueOf(valor);
              escoger.setAttribute("escoger",stropc);
              return mapping.findForward("insert");
               
            }else
            {
              return mapping.findForward("success");
            }
            
            
        
        }else
        {
            session.setAttribute("Error","1");
            return mapping.findForward("success");
                    
        }
    }finally
    {
      try{
        cf.closeConnection(objCons.getConn());
      }catch(SQLException e)
      {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSUB - cwsub ********/");
      }
    }
  }
}